﻿/** jQuery EasyUI 1.3.6 */
(function($) {
	var resizing = false;
	function setSize(target) {
		var layout = $.data(target, "layout");
		var opts = layout.options;
		var panels = layout.panels;
		var cc = $(target);
		if (target.tagName == "BODY") {
			cc._fit();
		} else {
			opts.fit ? cc.css(cc._fit()) : cc._fit(false);
		}
		var position = {
			top : 0,
			left : 0,
			width : cc.width(),
			height : cc.height()
		};
		positionMaxHeight(isVisible(panels.expandNorth) ? panels.expandNorth : panels.north, "n");
		positionMaxHeight(isVisible(panels.expandSouth) ? panels.expandSouth : panels.south, "s");
		positionMinWidth(isVisible(panels.expandEast) ? panels.expandEast : panels.east, "e");
		positionMinWidth(isVisible(panels.expandWest) ? panels.expandWest : panels.west, "w");
		panels.center.panel("resize", position);
		function maxHeight(panelBody) {
			var hopts = panelBody.panel("options");
			return Math.min(Math.max(hopts.height, hopts.minHeight), hopts.maxHeight);
		}
		;
		function minWidth(panelBody) {
			var wopts = panelBody.panel("options");
			return Math.min(Math.max(wopts.width, wopts.minWidth), wopts.maxWidth);
		}
		;
		function positionMaxHeight(panelBody, ns) {
			if (!panelBody.length || !isVisible(panelBody)) {
				return;
			}
			var popts = panelBody.panel("options");
			var maxHei = maxHeight(panelBody);
			panelBody.panel("resize", {
				width : cc.width(),
				height : maxHei,
				left : 0,
				top : (ns == "n" ? 0 : cc.height() - maxHei)
			});
			position.height -= maxHei;
			if (ns == "n") {
				position.top += maxHei;
				if (!popts.split && popts.border) {
					position.top--;
				}
			}
			if (!popts.split && popts.border) {
				position.height++;
			}
		}
		;
		function positionMinWidth(panelBody, ew) {
			if (!panelBody.length || !isVisible(panelBody)) {
				return;
			}
			var popts = panelBody.panel("options");
			var minWid = minWidth(panelBody);
			panelBody.panel("resize", {
				width : minWid,
				height : position.height,
				left : (ew == "e" ? cc.width() - minWid : 0),
				top : position.top
			});
			position.width -= minWid;
			if (ew == "w") {
				position.left += minWid;
				if (!popts.split && popts.border) {
					position.left--;
				}
			}
			if (!popts.split && popts.border) {
				position.width++;
			}
		}
		;
	}
	;
	function init(target) {
		var cc = $(target);
		cc.addClass("layout");
		function _17(cc) {
			cc.children("div").each(function() {
				var panelregion = $.fn.layout.parsePanelOptions(this);
				if ("north,south,east,west,center".indexOf(panelregion.region) >= 0) {
					createPanel(target, panelregion, this);
				}
			});
		};
		cc.children("form").length ? _17(cc.children("form")) : _17(cc);
		cc.append("<div class=\"layout-split-proxy-h\"></div><div class=\"layout-split-proxy-v\"></div>");
		cc.bind("_resize", function(e, _19) {
			var opts = $.data(target, "layout").options;
			if (opts.fit == true || _19) {
				setSize(target);
			}
			return false;
		});
	};
	function createPanel(target, region, el) {
		region.region = region.region || "center";
		var panels = $.data(target, "layout").panels;
		var cc = $(target);
		var dir = region.region;
		if (panels[dir].length) {
			return;
		}
		var panelBody = $(el);
		if (!panelBody.length) {
			panelBody = $("<div></div>").appendTo(cc);
		}
		var panelParam = $.extend({}, $.fn.layout.paneldefaults, {
			width : (panelBody.length ? parseInt(panelBody[0].style.width) || panelBody.outerWidth() : "auto"),
			height : (panelBody.length ? parseInt(panelBody[0].style.height) || panelBody.outerHeight() : "auto"),
			doSize : false,
			collapsible : true,
			cls : ("layout-panel layout-panel-" + dir),
			bodyCls : "layout-body",
			onOpen : function() {
				var panelTool = $(this).panel("header").children("div.panel-tool");
				panelTool.children("a.panel-tool-collapse").hide();
				var constant = {
					north : "up",
					south : "down",
					east : "right",
					west : "left"
				};
				if (!constant[dir]) {
					return;
				}
				var btnClass = "layout-button-" + constant[dir];
				var btn = panelTool.children("a." + btnClass);
				if (!btn.length) {
					btn = $("<a href=\"javascript:void(0)\"></a>").addClass(btnClass).appendTo(panelTool);
					btn.bind("click", {
						dir : dir
					}, function(e) {
						collapsePanel(target, e.data.dir);
						return false;
					});
				}
				$(this).panel("options").collapsible ? btn.show() : btn.hide();
			}
		}, region);
		panelBody.panel(panelParam);
		panels[dir] = panelBody;
		if (panelBody.panel("options").split) {
			var panel = panelBody.panel("panel");
			panel.addClass("layout-split-" + dir);
			var handles = "";
			if (dir == "north") {
				handles = "s";
			}
			if (dir == "south") {
				handles = "n";
			}
			if (dir == "east") {
				handles = "w";
			}
			if (dir == "west") {
				handles = "e";
			}
			panel.resizable($.extend({},{
				handles : handles,
				onStartResize : function(e) {
					resizing = true;
					if (dir == "north" || dir == "south") {
						var proxy = $(">div.layout-split-proxy-v",target);
					} else {
						var proxy = $(">div.layout-split-proxy-h",target);
					}
					var top = 0, left = 0, width = 0, height = 0;
					var pos = {
						display : "block"
					};
					if (dir == "north") {
						pos.top = parseInt(panel.css("top"))+ panel.outerHeight() - proxy.height();
						pos.left = parseInt(panel.css("left"));
						pos.width = panel.outerWidth();
						pos.height = proxy.height();
					} else {if (dir == "south") {
						pos.top = parseInt(panel.css("top"));
						pos.left = parseInt(panel.css("left"));
						pos.width = panel.outerWidth();
						pos.height = proxy.height();
					} else {if (dir == "east") {
						pos.top = parseInt(panel.css("top")) || 0;
						pos.left = parseInt(panel.css("left")) || 0;
						pos.width = proxy.width();
						pos.height = panel.outerHeight();
					} else {if (dir == "west") {
						pos.top = parseInt(panel.css("top")) || 0;
						pos.left = panel.outerWidth()- proxy.width();
						pos.width = proxy.width();
						pos.height = panel.outerHeight();
					}}}}
					proxy.css(pos);
					$("<div class=\"layout-mask\"></div>").css({
						left : 0,
						top : 0,
						width : cc.width(),
						height : cc.height()
					}).appendTo(cc);
				},
				onResize : function(e) {
					if (dir == "north" || dir == "south") {
						var proxy = $(">div.layout-split-proxy-v", target);
						proxy.css("top", e.pageY - $(target).offset().top - proxy.height() / 2);
					} else {
						var proxy = $(">div.layout-split-proxy-h", target);
						proxy.css("left", e.pageX - $(target).offset().left - proxy.width() / 2);
					}
					return false;
				},
				onStopResize : function(e) {
					cc.children("div.layout-split-proxy-v,div.layout-split-proxy-h").hide();
					panelBody.panel("resize", e.data);
					setSize(target);
					resizing = false;
					cc.find(">div.layout-mask").remove();
				}
			}, region));
		}
	}
	;
	function removePanel(target, region) {
		var panels = $.data(target, "layout").panels;
		if (panels[region].length) {
			panels[region].panel("destroy");
			panels[region] = $();
			var expandRegion = "expand" + region.substring(0, 1).toUpperCase() + region.substring(1);
			if (panels[expandRegion]) {
				panels[expandRegion].panel("destroy");
				panels[expandRegion] = undefined;
			}
		}
	}
	;
	function collapsePanel(target, region, animate) {
		if (animate == undefined) {
			animate = "normal";
		}
		var panels = $.data(target, "layout").panels;
		var p = panels[region];
		var pOpts = p.panel("options");
		if (pOpts.onBeforeCollapse.call(p) == false) {
			return;
		}
		var expandRegion = "expand" + region.substring(0, 1).toUpperCase() + region.substring(1);
		if (!panels[expandRegion]) {
			panels[expandRegion] = createExpandPanel(region);
			panels[expandRegion].panel("panel").bind("click",
				function() {
					var regionHandle = setCollapseRegionHandles();
					p.panel("expand", false).panel("open").panel("resize", regionHandle.collapse);
					p.panel("panel").animate(regionHandle.expand,function() {
						$(this).unbind(".layout").bind("mouseleave.layout", {
							region : region
						}, function(e) {
							if (resizing == true) {return;}
							collapsePanel(target, e.data.region);
						});
					});
				return false;
			});
		}
		var regionHandles = setCollapseRegionHandles();
		if (!isVisible(panels[expandRegion])) {
			panels.center.panel("resize", regionHandles.resizeC);
		}
		p.panel("panel").animate(regionHandles.collapse, animate, function() {
			p.panel("collapse", false).panel("close");
			panels[expandRegion].panel("open").panel("resize", regionHandles.expandP);
			$(this).unbind(".layout");
		});
		function createExpandPanel(dir) {
			var iconCls;
			if (dir == "east") {
				iconCls = "layout-button-left";
			} else { if (dir == "west") {
					iconCls = "layout-button-right";
			} else { if (dir == "north") {
						iconCls = "layout-button-down";
			} else { if (dir == "south") {
							iconCls = "layout-button-up";
			}}}}
			var p = $("<div></div>").appendTo(target);
			p.panel($.extend({}, $.fn.layout.paneldefaults, {
				cls : ("layout-expand layout-expand-" + dir),
				title : "&nbsp;",
				closed : true,
				minWidth : 0,
				minHeight : 0,
				doSize : false,
				tools : [ {
					iconCls : iconCls,
					handler : function() {
						expandPanel(target, region);
						return false;
					}
				} ]
			}));
			p.panel("panel").hover(function() {
				$(this).addClass("layout-expand-over");
			}, function() {
				$(this).removeClass("layout-expand-over");
			});
			return p;
		};
		function setCollapseRegionHandles() {
			var cc = $(target);
			var cpopts = panels.center.panel("options");
			var collSize = pOpts.collapsedSize;
			if (region == "east") {
				var ww = cpopts.width + pOpts.width - collSize;
				if (pOpts.split || !pOpts.border) {
					ww++;
				}
				return {
					resizeC : {
						width : ww
					},
					expand : {
						left : cc.width() - pOpts.width
					},
					expandP : {
						top : cpopts.top,
						left : cc.width() - collSize,
						width : collSize,
						height : cpopts.height
					},
					collapse : {
						left : cc.width(),
						top : cpopts.top,
						height : cpopts.height
					}
				};
			} else {
				if (region == "west") {
					var ww = cpopts.width + pOpts.width - collSize;
					if (pOpts.split || !pOpts.border) {
						ww++;
					}
					return {
						resizeC : {
							width : ww,
							left : collSize - 1
						},
						expand : {
							left : 0
						},
						expandP : {
							left : 0,
							top : cpopts.top,
							width : collSize,
							height : cpopts.height
						},
						collapse : {
							left : -pOpts.width,
							top : cpopts.top,
							height : cpopts.height
						}
					};
				} else {
					if (region == "north") {
						var hh = cpopts.height;
						if (!isVisible(panels.expandNorth)) {
							hh += pOpts.height - collSize + ((pOpts.split || !pOpts.border) ? 1 : 0);
						}
						panels.east.add(panels.west).add(panels.expandEast).add(panels.expandWest).panel("resize", {
							top : collSize - 1,
							height : hh
						});
						return {
							resizeC : {
								top : collSize - 1,
								height : hh
							},
							expand : {
								top : 0
							},
							expandP : {
								top : 0,
								left : 0,
								width : cc.width(),
								height : collSize
							},
							collapse : {
								top : -pOpts.height,
								width : cc.width()
							}
						};
					} else {
						if (region == "south") {
							var hh = cpopts.height;
							if (!isVisible(panels.expandSouth)) {
								hh += pOpts.height - collSize + ((pOpts.split || !pOpts.border) ? 1 : 0);
							}
							panels.east.add(panels.west).add(panels.expandEast).add(panels.expandWest).panel("resize", {
								height : hh
							});
							return {
								resizeC : {
									height : hh
								},
								expand : {
									top : cc.height() - pOpts.height
								},
								expandP : {
									top : cc.height() - collSize,
									left : 0,
									width : cc.width(),
									height : collSize
								},
								collapse : {
									top : cc.height(),
									width : cc.width()
								}
							};
						}
					}
				}
			}
		};
	};
	function expandPanel(target, region) {
		var panels = $.data(target, "layout").panels;
		var p = panels[region];
		var popts = p.panel("options");
		if (popts.onBeforeExpand.call(p) == false) {
			return;
		}
		var regionHandle = setExpandRegionHandles();
		var expandRegion = "expand" + region.substring(0, 1).toUpperCase() + region.substring(1);
		if (panels[expandRegion]) {
			panels[expandRegion].panel("close");
			p.panel("panel").stop(true, true);
			p.panel("expand", false).panel("open").panel("resize", regionHandle.collapse);
			p.panel("panel").animate(regionHandle.expand, function() {
				setSize(target);
			});
		}
		function setExpandRegionHandles() {
			var cc = $(target);
			var cpopts = panels.center.panel("options");
			if (region == "east" && panels.expandEast) {
				return {
					collapse : {
						left : cc.width(),
						top : cpopts.top,
						height : cpopts.height
					},
					expand : {
						left : cc.width() - panels["east"].panel("options").width
					}
				};
			} else {
				if (region == "west" && panels.expandWest) {
					return {
						collapse : {
							left : -panels["west"].panel("options").width,
							top : cpopts.top,
							height : cpopts.height
						},
						expand : {
							left : 0
						}
					};
				} else {
					if (region == "north" && panels.expandNorth) {
						return {
							collapse : {
								top : -panels["north"].panel("options").height,
								width : cc.width()
							},
							expand : {
								top : 0
							}
						};
					} else {
						if (region == "south" && panels.expandSouth) {
							return {
								collapse : {
									top : cc.height(),
									width : cc.width()
								},
								expand : {
									top : cc.height() - panels["south"].panel("options").height
								}
							};
						}
					}
				}
			}
		};
	};
	function isVisible(panelBody) {
		if (!panelBody) {
			return false;
		}
		if (panelBody.length) {
			return panelBody.panel("panel").is(":visible");
		} else {
			return false;
		}
	};
	function collapseAll(target) {
		var panel = $.data(target, "layout").panels;
		if (panel.east.length && panel.east.panel("options").collapsed) {
			collapsePanel(target, "east", 0);
		}
		if (panel.west.length && panel.west.panel("options").collapsed) {
			collapsePanel(target, "west", 0);
		}
		if (panel.north.length && panel.north.panel("options").collapsed) {
			collapsePanel(target, "north", 0);
		}
		if (panel.south.length && panel.south.panel("options").collapsed) {
			collapsePanel(target, "south", 0);
		}
	};
	$.fn.layout = function(method, param) {
		if (typeof method == "string") {
			return $.fn.layout.methods[method](this, param);
		}
		method = method || {};
		return this.each(function() {
			var layout = $.data(this, "layout");
			if (layout) {
				$.extend(layout.options, method);
			} else {
				var opts = $.extend({}, $.fn.layout.defaults, $.fn.layout.parseOptions(this), method);
				$.data(this, "layout", {
					options : opts,
					panels : {
						center : $(),
						north : $(),
						south : $(),
						east : $(),
						west : $()
					}
				});
				init(this);
			}
			setSize(this);
			collapseAll(this);
		});
	};
	$.fn.layout.methods = {
		resize : function(jq) {
			return jq.each(function() {
				setSize(this);
			});
		},
		panel : function(jq, param) {
			return $.data(jq[0], "layout").panels[param];
		},
		collapse : function(jq, param) {
			return jq.each(function() {
				collapsePanel(this, param);
			});
		},
		expand : function(jq, region) {
			return jq.each(function() {
				expandPanel(this, region);
			});
		},
		add : function(jq, param) {
			return jq.each(function() {
				createPanel(this, param);
				setSize(this);
				if ($(this).layout("panel", param.region).panel(
						"options").collapsed) {
					collapsePanel(this, param.region, 0);
				}
			});
		},
		remove : function(jq, region) {
			return jq.each(function() {
				removePanel(this, region);
				setSize(this);
			});
		}
	};
	$.fn.layout.parseOptions = function(target) {
		return $.extend({}, $.parser.parseOptions(target, [ {
			fit : "boolean"
		} ]));
	};
	$.fn.layout.defaults = {
		fit : false
	};
	$.fn.layout.parsePanelOptions = function(target) {
		return $.extend({}, $.fn.panel.parseOptions(target), $.parser.parseOptions(target, [ "region", {
			split : "boolean",
			collpasedSize : "number",
			minWidth : "number",
			minHeight : "number",
			maxWidth : "number",
			maxHeight : "number"
		} ]));
	};
	$.fn.layout.paneldefaults = $.extend({}, $.fn.panel.defaults, {
		region : null,
		split : false,
		collapsedSize : 28,
		minWidth : 10,
		minHeight : 10,
		maxWidth : 10000,
		maxHeight : 10000
	});
})(jQuery);
